var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
var mysql = require('mysql');
var bdconfig = require('../db/dbconfig');
var usersql = require('./usersql');
var pool = mysql.createPool(bdconfig.mysql);
var dataJSON = function(res,ret){
	if (typeof ret === 'undefined') {
		res.send(JSON.stringify({code: '-200',msg:'炒作失败'}));
	}else{ 
       // var x = {status:0,message:ret}
		res.send(JSON.stringify(ret));
	}
}


router.get('/getprodlist',function(req,res){
	res.setHeader("Access-Control-Allow-Origin", "*");
	// var parm = req.query || req.params
	pool.getConnection(function(err,connection){
		if (err) throw err;

		connection.query(usersql.queryAll,function(err,result){
			if(err) throw err;
			result={status:0,message:result}
			dataJSON(res,result)

			connection.release();
		})
	}) 
})

router.post('/addproduct',function(req,res){
	var name = req.body.name
    var ctime = new Date()
    res.setHeader("Access-Control-Allow-Origin", "*");
	pool.getConnection(function(err,connection){
		if (err) throw err;
		connection.query(usersql.insert,[name,ctime],function(err,result){
			if (err) throw err;
			result={status:0,message:"添加成功" }

			dataJSON(res,result)

			connection.release();

		})

	})
      
})

router.get('/delproduct/:id',function(req,res){
	 res.setHeader("Access-Control-Allow-Origin", "*");
	var id = req.params.id
	pool.getConnection(function(err,connection){
		if (err) throw err;
		connection.query(usersql.delect,[id],function(err,result){
			if (err) throw err;
			result={status:0,message:"删除成功1" };
			dataJSON(res,result)

			connection.release();

		})
	})
	



})
module.exports = router;
